package com.zrt.service.impl;

import com.zrt.dao.A_CDao;
import com.zrt.dao.AuthorDao;
import com.zrt.dao.CitationDao;
import com.zrt.dao.ParseDao;
import com.zrt.domain.*;
import com.zrt.service.ParseService;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class ParseServiceImpl implements ParseService {

    @Autowired
    private AuthorDao authorDao;

    @Autowired
    private ParseDao parseDao;

    @Autowired
    private CitationDao citationDao;

    @Autowired
    private A_CDao a_cDao;

    public Author parseAuthor(InputAndOutput inputAndOutput){
        String input = inputAndOutput.getInput();

        String file = parseDao.parseFile(input);

        String author = parseDao.parseDetailForAuthor(file);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long times = System.currentTimeMillis();
        authorDao.insert(new Author(format.format(times),author));

        String reference = parseDao.parseDetailForReference(file);
        List<Citation> citationList = parseDao.sortingCitation(reference);
        for (Citation citation : citationList){
            citationDao.insert(citation);
            a_cDao.insert(new Author_Reference(format.format(times),citation.getId()));
        }

        Author author1 = authorDao.selectAllAuthorAndCitationById(format.format(times));

        return author1;
    }

    public List<Author> parseAuthorList(InputAndOutput inputAndOutput){
        String input = inputAndOutput.getInput();

        String file = parseDao.parseFile(input);

        String author = parseDao.parseDetailForAuthor(file);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long times = System.currentTimeMillis();
        authorDao.insert(new Author(format.format(times),author));

        String reference = parseDao.parseDetailForReference(file);
        List<Citation> citationList = parseDao.sortingCitation(reference);
        for (Citation citation : citationList){
            citationDao.insert(citation);
            a_cDao.insert(new Author_Reference(format.format(times),citation.getId()));
        }

        List<Author> authorList = authorDao.selectAllAuthorAndCitationListById(format.format(times));

        return authorList;
    }

    @Override
    public List<Author> selectAllAuthorAndCitation() {
        List<Author> authorList = authorDao.selectAllAuthorAndCitation();
        return authorList;
    }

    @Override
    public List<Author> selectLikeAllAuthorAndCitation(String authorOrTittle) {
        List<Author> authorList = authorDao.selectLikeAllAuthorAndCitation(authorOrTittle);
        return authorList;
    }



}
